home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Atlanta_1990 / Atlanta-Devcon.1 / Libraries / Intuition / boopsi / demo3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-26  |  10.1 KB  |  402 lines

  1. /* demo3.c -- demonstration of         :ts=8
  2.  * Basic Object Oriented Programming System for Intuition
  3.  *
  4.  * This demo shows gadgets and images created using pre-defined
  5.  * boopsi classes, interconnected using boopsi interconnections
  6.  * and a "model".
  7.  *
  8.  * The improvement over demo 2 is that the application only
  9.  * receives messages it is specifically interested in.  All
  10.  * inter-gadget traffic is handled (synchronously) among 
  11.  * the objects involved.
  12.  */
  13.  
  14. /*
  15. Copyright (c) 1989 Commodore-Amiga, Inc.
  16.  
  17. Executables based on this information may be used in software
  18. for Commodore Amiga computers. All other rights reserved.
  19. This information is provided "as is"; no warranties are made.
  20. All use is at your own risk, and no liability or responsibility
  21. is assumed.
  22. */
  23.  
  24. #include "sysall.h"
  25. #include <intuition/icclass.h>
  26. #include "mymodel.h"
  27.  
  28. #define D(x)    x
  29.  
  30. struct  IntuitionBase   *IntuitionBase;
  31. struct  GfxBase         *GfxBase;
  32. struct  Library         *UtilityBase;
  33.  
  34. /* from varargs.c -- interface to NewObjectA()    */
  35. Object    *NewObject();
  36.  
  37. ULONG    myiflags =  CLOSEWINDOW | IDCMPUPDATE;
  38.  
  39. Object        *mymodel = NULL; /* the repository of the "Current Value" */
  40. void    *MyModClass = NULL;
  41. void    *initMyModClass();
  42.  
  43.  
  44. struct Gadget    *propg = NULL;
  45. struct Gadget    *stringg = NULL;
  46. struct Gadget    *uparrowg = NULL;
  47. struct Gadget    *downarrowg = NULL;
  48. struct Gadget    *mygadgets = NULL;        /* linked list    */
  49.  
  50. /* pictures for arrows    */
  51. struct Image    *upimage = NULL;
  52. struct Image    *downimage = NULL;
  53.  
  54. /* some static layout and setup constants, for now    */
  55. #define GTOP        (44)
  56. #define ARROWLEFT    (180)
  57. #define PWIDTH        (120)    /* width of horizontal propslider    */
  58. #define SWIDTH        (50)
  59. #define PROPRANGE    (20)
  60. #define INITVAL        (0)    /* initial value of string and slider    */
  61.  
  62. enum gadgetids {
  63.     gUp = 1,
  64.     gDown,
  65.     gSlider,
  66.     gString,
  67. };
  68.  
  69.  
  70. /****************************************************************/
  71. /*  mapping tag lists                        */
  72. /****************************************************************/
  73.  
  74. /* for IDCMPUPDATE    */
  75. struct TagItem    model2me[] = {
  76.     /* {MYMODA_CURRVAL, MYMODA_CURRVAL }, */
  77.     {MYMODA_CURRVAL, ICSPECIAL_CODE },
  78.     /* put (16 bits of) currval into IntuiMessage.Code    */
  79.     { TAG_END, }
  80. };
  81.  
  82. struct TagItem    slider2model[] = {
  83.     {PGA_TOP, MYMODA_CURRVAL},
  84.     {TAG_END, }
  85. };
  86.  
  87. struct TagItem    model2slider[] = {
  88.     {MYMODA_CURRVAL, PGA_TOP},
  89.     {MYMODA_RANGE, PGA_TOTAL },
  90.     {TAG_END, }
  91. };
  92.  
  93. struct TagItem    string2model[] = {
  94.     {STRINGA_LongVal, MYMODA_CURRVAL},
  95.     {TAG_END, }
  96. };
  97.  
  98. struct TagItem    model2string[] = {
  99.     {MYMODA_CURRVAL, STRINGA_LongVal},
  100.     {TAG_END, }
  101. };
  102.  
  103. struct TagItem    uparrow2model[] = {
  104.     {GA_ID, MYMODA_INCRSTROBE},
  105.     {TAG_END, }
  106. };
  107.  
  108. struct TagItem    downarrow2model[] = {
  109.     {GA_ID, MYMODA_DECRSTROBE},
  110.     {TAG_END, }
  111. };
  112.  
  113. /****************************************************************/
  114. /* tag lists for creating objects                */
  115. /****************************************************************/
  116.  
  117. struct TagItem modeltags[] = {
  118.     { ICA_TARGET,    ICTARGET_IDCMP},    /* talk to me    */
  119.     { ICA_MAP,        (ULONG) &model2me[0]},
  120.     {TAG_END ,}
  121. };
  122.  
  123. struct TagItem    proptags[] = {
  124.     {GA_TOP,        GTOP},
  125.     {GA_WIDTH,        PWIDTH},    /* height to be specified    */
  126.     {GA_ID,        gSlider},
  127.  
  128.     /* {ICA_TARGET,    XXX }, * will be model object    */
  129.     {ICA_MAP,        (ULONG) &slider2model[0]},
  130.  
  131.     {PGA_FREEDOM,    FREEHORIZ},
  132.     {PGA_VISIBLE,    1},        /* want an integer value slider    */
  133.  
  134. #if 0    /* the whole set of gadgets will be initialized together    */
  135.     {PGA_TOP,        INITVAL},    /* "top" in the scroller sense    */
  136.     {PGA_TOTAL,        PROPRANGE},
  137. #endif
  138.  
  139.     {TAG_END ,}
  140. };
  141.  
  142. struct TagItem    stringtags[] = {
  143.     {GA_ID,        gString},
  144.     {GA_TOP,        GTOP},
  145.     {GA_WIDTH,        SWIDTH},
  146.     {GA_HEIGHT,        12},        /* fix this    */
  147.  
  148.     /* {ICA_TARGET,    XXXX },        will be model object    */
  149.     {ICA_MAP,        (ULONG) &string2model[0]},
  150.  
  151.     {STRINGA_MaxChars,    10},
  152.     {STRINGA_LongVal,    INITVAL},    /* make it an integer gadget */
  153.     {STRINGA_Justification,
  154.                 STRINGRIGHT},
  155.     {TAG_END, }
  156. };
  157.  
  158. struct TagItem    uparrowtags[] = {
  159.     {GA_TOP,        GTOP},
  160.     /* {ICA_TARGET,    ICTARGET_IDCMP}, will be model object    */
  161.     {ICA_MAP,        (ULONG) &uparrow2model[0]},
  162.     {TAG_END, }
  163. };
  164.  
  165. struct TagItem    downarrowtags[] = {
  166.     {GA_TOP,        GTOP},
  167.     /* {ICA_TARGET,    ICTARGET_IDCMP}, will be model object    */
  168.     {ICA_MAP,        (ULONG) &downarrow2model[0]},
  169.     {TAG_END, }
  170. };
  171.  
  172. #define MYWINDOWTITLE    "boopsi Demo 3"
  173.  
  174. main()
  175. {
  176.     struct DrawInfo    *GetScreenDrawInfo();
  177.     struct Window     *OpenWindowTags();    /* in varargs.c for now */
  178.  
  179.     struct Gadget    *tmpgad;
  180.     struct Window    *w;
  181.     struct DrawInfo    *drinfo;
  182.  
  183.     Object        *ic;
  184.  
  185.     if (!(UtilityBase=(struct Library *)OpenLibrary("utility.library",36L)))
  186.     { cleanup("no V36 utility library\n"); }
  187.  
  188.     if (!(IntuitionBase = 
  189.     (struct IntuitionBase *) OpenLibrary("intuition.library", 36L)))
  190.     { cleanup("no V36 intuition library\n"); }
  191.  
  192.     if (!(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 36L)))
  193.     { cleanup("no V36 graphics library\n"); }
  194.  
  195.     w = OpenWindowTags( NULL, 
  196.         WA_Title,    MYWINDOWTITLE,
  197.         WA_SimpleRefresh, TRUE,
  198.         WA_NoCareRefresh, TRUE,
  199.         WA_DepthGadget,    TRUE,
  200.         WA_DragBar,    TRUE,
  201.         WA_Left,    300,
  202.         WA_Top,        50,
  203.         WA_Width,    280,
  204.         WA_Height,    120,
  205.         WA_IDCMP,    myiflags,
  206.         WA_CloseGadget,    TRUE,
  207.             TAG_END );
  208.     D( printf("window at %lx\n", w ) );
  209.  
  210.     if ( w == NULL) cleanup( "couldn't open my window.\n");
  211.     drinfo = GetScreenDrawInfo( w->WScreen );
  212.  
  213.     /* get images for the up and down arrows, sensitive
  214.      * to depth and pen specs for current screen (we'll be
  215.      * adding resolution/size selection later).
  216.      */
  217.     upimage = (struct Image *) NewObject( NULL, "sysiclass",
  218.     SYSIA_Size,    0,        /* normal "medium-res" for now */
  219.     SYSIA_DrawInfo, drinfo,
  220.     SYSIA_Which,    UPIMAGE,
  221.         TAG_END );
  222.  
  223.     downimage = (struct Image *) NewObject( NULL, "sysiclass",
  224.     SYSIA_Size,    0,        /* normal "medium-res" for now */
  225.     SYSIA_Which,    DOWNIMAGE,
  226.     SYSIA_DrawInfo, drinfo,
  227.         TAG_END );
  228.  
  229.  
  230.     /* get "model" object which is the repository of our "current
  231.      * value" and is the hub of object interconnection.
  232.      * This thing also is used to free icclass objects,
  233.      * so we'd better make sure it got allocated.
  234.      */
  235.     MyModClass = initMyModClass();    /* private class    */
  236.     D( printf("get model object, class at %lx\n", MyModClass ) );
  237.  
  238.     mymodel = (Object *) NewObjectA( MyModClass, NULL, modeltags );
  239.     D( printf("model at %lx\n", mymodel ) );
  240.  
  241.     if ( ! mymodel ) cleanup( "couldn't get model object" );
  242.  
  243.     /* make gadgets, link into list (easier starting with Beta 4) */
  244.     tmpgad = (struct Gadget *) &mygadgets;
  245.  
  246.     D( printf("get gadgets\n"));
  247.  
  248.     downarrowg = (struct Gadget *) NewObject( NULL, "buttongclass",
  249.     GA_IMAGE,     downimage,
  250.     GA_TOP,        GTOP,
  251.     GA_LEFT,    ARROWLEFT,
  252.     GA_ID,        gDown,
  253.     GA_PREVIOUS,    tmpgad,
  254.     /* interconnections ...    */
  255.     ICA_TARGET,    mymodel,
  256.     ICA_MAP,    &downarrow2model[0],
  257.     TAG_END );
  258.     D( printf("downgadget at %lx\n", downarrowg ));
  259.  
  260.     /* get up/down arrow button gadgets    */
  261.     uparrowg = (struct Gadget *) NewObject( NULL, "buttongclass",
  262.     GA_IMAGE,     upimage,
  263.     GA_TOP,        GTOP,
  264.     GA_LEFT,    ARROWLEFT + (downarrowg? downarrowg->Width: 0),
  265.     GA_ID,        gUp,
  266.     GA_PREVIOUS,    tmpgad,
  267.     /* interconnections ...    */
  268.     ICA_MAP,    &uparrow2model[0],
  269.     ICA_TARGET,    mymodel,
  270.     TAG_END );
  271.     D( printf("upgadget at %lx\n", uparrowg ));
  272.  
  273.     propg = (struct Gadget *) NewObject( NULL, "propgclass",
  274.     GA_LEFT,    ARROWLEFT - PWIDTH,
  275.     GA_HEIGHT,    downarrowg? downarrowg->Height: 20,
  276.     GA_PREVIOUS,    tmpgad,
  277.  
  278.     ICA_TARGET,    mymodel,
  279.     TAG_MORE,    proptags,
  280.     TAG_END );
  281.     D( printf( "prop gadget returned: %lx\n", propg ) );
  282.  
  283.     stringg  = (struct Gadget *) NewObject( NULL, "strgclass",
  284.         GA_LEFT,    propg? (propg->LeftEdge - SWIDTH): 20,
  285.     GA_PREVIOUS,    tmpgad,
  286.  
  287.     ICA_TARGET,    mymodel,
  288.     TAG_MORE,    stringtags,
  289.     TAG_END );
  290.     D( printf( "string gadget at %lx\n", stringg ) );
  291.  
  292.     /*
  293.      * We now have all the gadgets talking to the model,
  294.      * but we need to create some little IC nodes for
  295.      * the model to update the string and propotional gadgets
  296.      */
  297.     ic = NewObject( NULL, ICCLASS,
  298.             ICA_TARGET,    stringg,
  299.         ICA_MAP,    model2string,
  300.         TAG_END );
  301.     DoMethod( mymodel, OM_ADDMEMBER, ic );
  302.  
  303.     ic = NewObject( NULL, ICCLASS,
  304.             ICA_TARGET,    propg,
  305.         ICA_MAP,    model2slider,
  306.         TAG_END );
  307.     DoMethod( mymodel, OM_ADDMEMBER, ic );
  308.  
  309.     D(printf("objects initialized\n"));
  310.  
  311.     AddGList( w, mygadgets, -1, -1, NULL );
  312.     RefreshGList( mygadgets, w, NULL, -1 );
  313.  
  314.     D( printf("gadgets added and refreshed \n") );
  315.  
  316.     /* although we're changing the attributes of
  317.      * the model, if we want the gadgets attached 
  318.      * to it to be able to refresh, we have to 
  319.      * use SetGadgetAttr()s rather than SetAttrs()
  320.      */
  321.     SetGadgetAttrs( mymodel, w, NULL,
  322.                 MYMODA_RANGE, PROPRANGE,
  323.             MYMODA_CURRVAL, PROPRANGE/2,
  324.             TAG_END );
  325.  
  326.     D( printf("have set range and initval\n" ) );
  327.  
  328.     goHandleWindow( w );
  329.  
  330.     RemoveGList( w, mygadgets, -1 );
  331.     FreeScreenDrawInfo( w->WScreen, drinfo );
  332.     CloseWindow( w );
  333.     cleanup( "all done" );
  334. }
  335.  
  336. cleanup( str )
  337. char    *str;
  338. {
  339.     struct Gadget    *tmpgad;
  340.  
  341.     if (str) printf("%s\n", str);
  342.  
  343.     D( printf("dispose objects\n") );
  344.     DisposeObject( mymodel );
  345.  
  346.     while ( mygadgets )
  347.     {
  348.     tmpgad = mygadgets->NextGadget;
  349.     DisposeObject( mygadgets );
  350.     mygadgets = tmpgad;
  351.     }
  352.  
  353.     DisposeObject( upimage);
  354.     DisposeObject( downimage);
  355.     D( printf("have disposed.\n") );
  356.  
  357.     if ( MyModClass ) freeMyModClass( MyModClass );
  358.  
  359.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  360.     if (GfxBase) CloseLibrary(GfxBase);
  361.     if (UtilityBase) CloseLibrary(UtilityBase);
  362.  
  363.     exit (0);
  364. }
  365.  
  366. goHandleWindow( w )
  367. struct Window    *w;
  368. {
  369.     struct TagItem    *FindTagItem();
  370.  
  371.     struct IntuiMessage *imsg;
  372.     struct TagItem    *tags;
  373.     ULONG        currval;
  374.     /* struct TagItem    *ti; */
  375.  
  376.     for(;;)
  377.     {
  378.     WaitPort( w->UserPort );
  379.     while ( imsg = (struct IntuiMessage *) GetMsg( w->UserPort ) )
  380.     {
  381.         switch ( imsg->Class )
  382.         {
  383.         case CLOSEWINDOW:
  384.             ReplyMsg( (struct Message *) imsg );
  385.         return;
  386.  
  387.         case IDCMPUPDATE:
  388.         tags = (struct TagItem *) imsg->IAddress;
  389.             D( printf("IDCMPUPDATE, quals %lx code (decimal) %ld\n",
  390.             imsg->Qualifier, imsg->Code ));
  391.  
  392.         GetAttr( MYMODA_CURRVAL, mymodel, &currval );
  393.         D( printf("Current value now %ld\n", currval ) );
  394.  
  395.         break;
  396.         }
  397.         ReplyMsg( (struct Message *) imsg );
  398.     }
  399.     }
  400. }
  401.  
  402.